package com.dlc.shop.delivery.api.manager;

import cn.hutool.core.collection.CollUtil;
import cn.hutool.core.map.MapUtil;
import com.dlc.shop.bean.app.dto.OrderSelfStationDto;
import com.dlc.shop.bean.app.dto.ShopCartItemDto;
import com.dlc.shop.bean.app.dto.ShopCartOrderMergerDto;
import com.dlc.shop.bean.dto.OrderStationProdDto;
import com.dlc.shop.bean.dto.StockPointSkuDTO;
import com.dlc.shop.bean.enums.DeliveryType;
import com.dlc.shop.bean.vo.UserDeliveryInfoVO;
import com.dlc.shop.common.constants.StockPointType;
import com.dlc.shop.delivery.common.service.OrderSelfStationService;
import com.dlc.shop.delivery.common.service.SameCityService;
import com.dlc.shop.service.StationService;
import com.dlc.shop.service.UserAddrService;
import lombok.AllArgsConstructor;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.stereotype.Component;

import java.util.*;

/**
 * @author 菠萝凤梨
 */
@Component
@AllArgsConstructor
public class StationOrderManager {
    private static final Logger logger = LoggerFactory.getLogger(StationOrderManager.class);
    public static final int INITIAL_CAPACITY = 16;
    private final OrderSelfStationService orderSelfStationService;
    private final UserAddrService userAddrService;
    private final SameCityService sameCityService;
    private final StationService stationService;

    public void handleStationOrder(ShopCartOrderMergerDto shopCartOrderMerger, List<ShopCartItemDto> shopCartItems, UserDeliveryInfoVO userDeliveryInfoVO) {
        Map<Long, OrderStationProdDto> orderStationSpuMap = this.getStationOrderStockMap(shopCartItems, userDeliveryInfoVO);
        if (MapUtil.isEmpty(orderStationSpuMap)) {
            if (Objects.nonNull(userDeliveryInfoVO.getOrderSelfStation())) {
                shopCartOrderMerger.setOrderSelfStationDto(userDeliveryInfoVO.getOrderSelfStation());
            }
            return;
        }
        userDeliveryInfoVO.setSeckillId(shopCartOrderMerger.getSeckillId());
        OrderSelfStationDto orderSelfStationVO = stationService.getOrderStation(userDeliveryInfoVO, new ArrayList<>(orderStationSpuMap.values()));
        userDeliveryInfoVO.setStationId(orderSelfStationVO.getStationId());
        userDeliveryInfoVO.setOrderSelfStation(orderSelfStationVO);
        shopCartOrderMerger.setOrderSelfStationDto(orderSelfStationVO);
        if (Objects.nonNull(userDeliveryInfoVO.getStationId())) {
            shopCartOrderMerger.setStationId(userDeliveryInfoVO.getStationId());

        }
    }

    private Map<Long, OrderStationProdDto> getStationOrderStockMap(List<ShopCartItemDto> shopCartItems, UserDeliveryInfoVO userDeliveryInfoVO) {
        Integer dvyType = userDeliveryInfoVO.getDvyTypes().get(0).getDvyType();
        if (Objects.nonNull(userDeliveryInfoVO.getStationId()) || !Objects.equals(dvyType, DeliveryType.STATION.getValue())) {
            return new HashMap<>(0);
        }

        List<ShopCartItemDto> shopCartItemList = new ArrayList<>(shopCartItems.size());
        shopCartItems.forEach(shopCartItemVO -> {
            if (CollUtil.isNotEmpty(shopCartItemVO.getComboShopCartItems())) {
                shopCartItemList.addAll(shopCartItemVO.getComboShopCartItems());
            } else {
                shopCartItemList.add(shopCartItemVO);
            }
        });
        Map<Long, OrderStationProdDto> orderStationSpuMap = new HashMap<>(shopCartItemList.size());
        for (ShopCartItemDto shopCartItemDto : shopCartItemList) {
            OrderStationProdDto orderStationProdDTO = orderStationSpuMap.get(shopCartItemDto.getSkuId());
            if (!orderStationSpuMap.containsKey(shopCartItemDto.getSkuId())) {
                orderStationProdDTO = new OrderStationProdDto();
                orderStationProdDTO.setSkuId(shopCartItemDto.getSkuId());
                orderStationProdDTO.setCount(0);
                orderStationProdDTO.setSpuMold(shopCartItemDto.getMold());
                orderStationProdDTO.setDefaultStockPointId(shopCartItemDto.getStockPointId());
                if (Objects.nonNull(userDeliveryInfoVO.getSeckillId())) {
                    // 筛选默认库存和门店仓库
                    List<StockPointSkuDTO> stockPointList = shopCartItemDto.getStockPointList().stream()
                            .filter(stockPointSkuVO -> Objects.equals(stockPointSkuVO.getType(), 0) || Objects.equals(stockPointSkuVO.getStockPointType(), StockPointType.STATION.getValue()))
                            .toList();
                    orderStationProdDTO.setStockPointList(stockPointList);
                }
                orderStationSpuMap.put(shopCartItemDto.getSkuId(), orderStationProdDTO);
            }
            orderStationProdDTO.setCount(orderStationProdDTO.getCount() + shopCartItemDto.getProdCount());
        }
        return orderStationSpuMap;
    }
}
